package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.PermissionDao;
import com.itheima.dao.RoleDao;
import com.itheima.dao.UserDao;
import com.itheima.pojo.Permission;
import com.itheima.pojo.Role;
import com.itheima.pojo.User;
import com.itheima.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Set;

/**
 * @author kabuda
 * @create 2022-08-25 10:09
 */
@Service(interfaceClass = UserService.class)
@Transactional
public class UserServiceImpl implements UserService {
    @Autowired
    UserDao userDao;
    @Autowired
    RoleDao roleDao;
    @Autowired
    PermissionDao permissionDao;
    //根据哦
    @Override
    public User findByUserName(String username) {
        User user = userDao.findByUserName(username);
        if(user == null){
            return null;
        }
        Set<Role> roles = roleDao.findByUserId(user.getId());
        if(roles!=null&&roles.size()>0){
            for (Role role : roles) {
                Set<Permission> permissions= permissionDao.findByRoleId(role.getId());
                if(permissions != null && permissions.size() > 0) {
                    role.setPermissions(permissions);
                }
            }
            user.setRoles(roles);
        }

        return user;
    }
}
